package com.hjm.project.bankExpenditure.gbTradeInfo.controller;

import java.util.List;
import java.util.Map;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.terminal.manufacturer.service.IManufacturerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.bankExpenditure.gbTradeInfo.domain.GbTradeInfo;
import com.hjm.project.bankExpenditure.gbTradeInfo.service.IGbTradeInfoService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import javax.annotation.Resource;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 钱宝官品交易信息Controller
 *
 * @author maker-platform
 * @date 2022-10-17
 */
@Controller
@RequestMapping("/bankExpenditure/gbTradeInfo")
public class GbTradeInfoController extends BaseController {
    private String prefix = "bankExpenditure/gbTradeInfo";

    @Resource
    private IGbTradeInfoService gbTradeInfoService;
    @Resource
    private IManufacturerService manufacturerService;

    @RequiresPermissions("bankExpenditure:gbTradeInfo:view")
    @GetMapping()
    public String gbTradeInfo(@RequestParam Map<String,String> params, ModelMap mmap) throws Exception {
        List<Map> manufacturer = manufacturerService.getManufacturer();
        mmap.put("manufacturer",manufacturer);
        if (StringUtils.isBlank(params.get("isShow"))) {
            mmap.put("start", DateUtils.getDate());
            mmap.put("end",DateUtils.getDate());
            mmap.put("isShow","1");
        }
        return prefix + "/gbTradeInfo";
    }
    @GetMapping("/gbTradeInfoOld")
    public String gbTradeInfoOld(ModelMap mmap) throws Exception {
        List<Map> manufacturer = manufacturerService.getManufacturer();
        mmap.put("manufacturer",manufacturer);
        return prefix + "/gbTradeInfoOld";
    }

    /**
     * 查询钱宝官品交易信息列表
     */
    @RequiresPermissions("bankExpenditure:gbTradeInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GbTradeInfo gbTradeInfo) {
        if (ShiroUtils.isSale()) {
            gbTradeInfo.setSaleAgentNum(ShiroUtils.getLoginAgentNum());
        }
        if (ShiroUtils.isAgent()) {
            gbTradeInfo.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }
        Map data = gbTradeInfoService.selectGbTradeInfoListCount(gbTradeInfo);
        startPage();
        List<GbTradeInfo> list = gbTradeInfoService.selectGbTradeInfoList(gbTradeInfo);
        return getDataTable(list,data);
    }

   /* @PostMapping("/listOld")
    @ResponseBody
    public TableDataInfo listOld(GbTradeInfo gbTradeInfo) {

        Map data = gbTradeInfoService.selectGbTradeInfoOldListCount(gbTradeInfo);
        startPage();
        List<GbTradeInfo> list = gbTradeInfoService.selectGbTradeInfoOldList(gbTradeInfo);
        return getDataTable(list,data);
    }*/

    /**
     * 交易导出
     */
    @Log(title = "交易导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GbTradeInfo gbTradeInfo) {
        if (ShiroUtils.isSale()) {
            gbTradeInfo.setSaleAgentNum(ShiroUtils.getLoginAgentNum());
        }

        if (ShiroUtils.isAgent()) {
            gbTradeInfo.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }

        List<GbTradeInfo> list = gbTradeInfoService.selectGbTradeInfoList(gbTradeInfo);
        ExcelUtil<GbTradeInfo> util = new ExcelUtil<GbTradeInfo>(GbTradeInfo.class);
        return util.exportExcel(list, "交易导出");
    }

    /**
     * 新增钱宝官品交易信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存钱宝官品交易信息
     */
    @RequiresPermissions("bankExpenditure:gbTradeInfo:add")
    @Log(title = "钱宝官品交易信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GbTradeInfo gbTradeInfo) {
        return toAjax(gbTradeInfoService.insertGbTradeInfo(gbTradeInfo));
    }

    /**
     * 修改钱宝官品交易信息
     */
    @GetMapping("/edit/{serial}")
    public String edit(@PathVariable("serial") String serial, ModelMap mmap) {
        GbTradeInfo gbTradeInfo = gbTradeInfoService.selectGbTradeInfoById(serial);
        mmap.put("gbTradeInfo", gbTradeInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存钱宝官品交易信息
     */
    @RequiresPermissions("bankExpenditure:gbTradeInfo:edit")
    @Log(title = "钱宝官品交易信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GbTradeInfo gbTradeInfo) {
        return toAjax(gbTradeInfoService.updateGbTradeInfo(gbTradeInfo));
    }

    /**
     * 删除钱宝官品交易信息
     */
    @RequiresPermissions("bankExpenditure:gbTradeInfo:remove")
    @Log(title = "钱宝官品交易信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gbTradeInfoService.deleteGbTradeInfoByIds(ids));
    }

    /**
     * 推送交易分润
     */
    @PostMapping("/sync")
    @ResponseBody
    public AjaxResult sync(GbTradeInfo gbTradeInfo){
        try {
            Map<String,String> result = gbTradeInfoService.sync(gbTradeInfo);
            if (result.get("code").equals("0")){
                return AjaxResult.success(result.get("msg"));
            }else{
                return AjaxResult.error(result.get("msg"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error("推送交易分润异常");
    }

}
